package com.example.hxpc.guidancemanagement.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

/**
 * Created by tao on 2017/12/5.
 */

public class CreateProjectActivity extends Activity implements View.OnClickListener ,BaseApi{
    private TextView te_title;
    private ImageView img_add , img_back;
    private Button bu_reset,bu_sure;
    private EditText edt_pro_name , edt_pro_num , edt_pro_teacher,edt_pro_path;
    private Spinner spinner;
    private String state;
    private OkHttpTools okHttpTools = new OkHttpTools();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.creat_project);
        initView();
    }

    //初始化组件
    public void initView(){
        te_title = (TextView) findViewById(R.id.te_title);
        te_title.setText("创建项目");
        img_add = (ImageView) findViewById(R.id.add_mark);
        img_add.setVisibility(View.GONE);
        img_back = (ImageView) findViewById(R.id.back);
        img_back.setOnClickListener(this);
        bu_reset = (Button) findViewById(R.id.reset);
        bu_reset.setOnClickListener(this);
        bu_sure = (Button) findViewById(R.id.ok);
        bu_sure.setOnClickListener(this);
        edt_pro_name = (EditText) findViewById(R.id.edit_pro_name);
        edt_pro_num = (EditText) findViewById(R.id.edit_pro_number);
        edt_pro_teacher = (EditText) findViewById(R.id.edit_pro_teacher);
        edt_pro_path = (EditText) findViewById(R.id.edit_pro_path);
        spinner = (Spinner) findViewById(R.id.spinner);
//        spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                state = (String) spinner.getSelectedItem();
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.reset:
                edt_pro_name.setText("");
                edt_pro_num.setText("");
                edt_pro_teacher.setText("");
                break;
            case R.id.ok:
                if (edt_pro_name.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"项目名称不为空");
                }else if (edt_pro_num.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请添加人数");
                }else if (edt_pro_teacher.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请添加老师");
                }else if (edt_pro_path.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请添加项目路径");
                }else {
                    okHttpTools.createProject(this,edt_pro_name.getText().toString(),edt_pro_num.getText().toString(),edt_pro_teacher.getText().toString(),spinner.getSelectedItem().toString(),edt_pro_path.getText().toString());
                }
                break;
        }
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {
        if (success.equals("true")&&!jsonBase.getMsg().equals("该项目已存在")){
            Intent intent = new Intent();
            intent.putExtra("name",jsonBase.getData().getProjectName());
            intent.putExtra("number",jsonBase.getData().getJoin_num());
            intent.putExtra("teacher",jsonBase.getData().getTeacher());
            intent.putExtra("state",jsonBase.getData().getState());
            intent.putExtra("path",jsonBase.getData().getPath());
            setResult(1,intent);
            finish();
            ToastTools.toastMsg(this,jsonBase.getMsg());
        }else {
            ToastTools.toastMsg(this,jsonBase.getMsg());
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }
}
